import asyncio
import time

import aiohttp


async def ping(url):
    async with aiohttp.ClientSession() as session:
        for i in range(10):
            x_request_start_time = int(time.time() * 1000)
            async with session.get(url, headers={"X-Request-Start-Time": str(x_request_start_time)}) as resp:
                x_request_arrive_time = int(resp.headers["X-Request-Arrive-Time"])
                x_response_start_time = int(resp.headers["X-Response-Start-Time"])
                x_response_arrive_time = int(time.time() * 1000)

            print(f"X-Request-Start-Time:\t{x_request_start_time}")
            print(f"X-Request-Arrive-Time:\t{x_request_arrive_time}")
            print(f"X-Response-Start-Time:\t{x_response_start_time}")
            print(f"X-Response-Arrive-Time: {x_response_arrive_time}")

            print("the delay from client to remote server is {}".format(x_request_arrive_time - x_request_start_time))
            print("the delay from remote server to client is {}".format(x_response_arrive_time - x_response_start_time))
            print("the delay of request process is {}".format(x_response_start_time - x_request_arrive_time))
            print("total delay is {}".format(x_response_arrive_time - x_request_start_time))


asyncio.run(ping("http://localhost:8000/test/ping"))
